/**
 * 模型拖拽上图功能示意
 */
import { inject, toRaw } from "vue"
import { entityKeys } from "@/common/symbolKeys"

const Cesium = DE.__namespace.Cesium

let leftClickTarget, rightClickTarget

// 全局定义图元标绘层
let actionPlotLayer

let viewer // 地图对象
let plot // 标绘对象

// 初始化当前业务
export function onMounted(mapInstance) {
  viewer = mapInstance // 记录map
  leftClickTarget = inject(entityKeys.LEFT_CLICK_TARGET)()
  rightClickTarget = inject(entityKeys.RIGHT_CLICK_TARGET)()
  plot = new DE.Plot(viewer, {
    deactivateDefaultStopAction: false
  })
}

// 释放当前业务
export function onUnmounted() {
  viewer = null
}

// 地图上标绘动作
export function deployActionPlot(plotType, plotName, actionPlotLayer) {
  if (!actionPlotLayer) {
    return false
  }
  window.$tooltipAutoHide = false
  plot &&
    plot.draw(plotType, (overlay) => {
      if (overlay) {
        overlay.setLabel(plotName, {
          showBackground: false,
          font: "12px monospace",
          outlineColor: Cesium.Color.fromCssColorString("white").withAlpha(0.5),
          style: Cesium.LabelStyle.FILL_AND_OUTLINE, // FILL_AND_OUTLINE
          outlineWidth: 3,
          fillColor: Cesium.Color.BLACK,
          horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
          verticalOrigin: Cesium.VerticalOrigin.CENTER,
          pixelOffset: new Cesium.Cartesian2(15, -15)
        })
        actionPlotLayer.addOverlay(overlay)
        // overlay.contextMenu = [
        //   {
        //     label: "编辑",
        //     show: () => {
        //       return !overlay.isEditing
        //     },
        //     callback: (e) => {
        //       plot.edit(overlay)
        //     }
        //   },
        //   {
        //     label: "结束编辑",
        //     show: () => {
        //       return overlay.isEditing
        //     },
        //     callback: (e) => {
        //       plot.stop()
        //     }
        //   }
        // ]
      }
    })
}
